<?php

namespace humhub\modules\api\controllers;

use humhub\modules\api\models\Ads;
use humhub\modules\file\models\File;
use Yii;
use humhub\modules\post\models\Post;


/**
 * 分享相关接口 app广告相关接口
 */
class ShareController extends Base4Controller
{

    /**
     * 获取分享内容的 URL
     *
     * GET api/share/url
     *
     * get.id POST的id
     *
     * @return array 返回用于分享的URL地址
     */
    public function actionUrl(){
        $id = (int)Yii::$app->request->get('id');
        $model = Post::findOne(['id' => $id]);
        if( ! $model ){
            throw new \yii\web\BadRequestHttpException('分享失败.');
        }

        return Array(
            'url' => 'http://www.uni-class.com',
        );
    }

    /**
     * 获取分享内容到H5的 URL
     *
     * GET api/share/data
     *
     * get.id POST的id
     *
     * get.uid userid
     *
     * @return array 返回用于分享的URL地址
     */
    public function actionData(){

        $id = (int)Yii::$app->request->get('id');
        $uid = (int)Yii::$app->request->get('uid');
        $model = Post::findOne(['id' => $id]);
        if( ! $model ){
            throw new \yii\web\BadRequestHttpException('找不到相关作业');
        }
        return Array(
            'url' => "http://www.uni-class.com/jobs/likeme/{$id}/{$uid}",
        );

    }

    /**
     * 获取app启动页广告数据
     *
     * GET api/share/ads
     *
     * @return array img 广告图片 url跳转网址
     */
    public function actionAds(){
        $data=Ads::find()->orderBy('time DESC')->limit(1)->one();
        if (isset($data['img'])){
            $pic=File::findOne(['guid'=>$data['img']]);
            $data['img']=$pic->getUrl();
        }
        return $data;
    }
}
